#include<bits/stdc++.h>
using namespace std;
int m,n,a[2010],d[2010];
char c[2010];
string s;
int main(){
	freopen("hw.in","r",stdin);
	freopen("hw.out","w",stdout);
	cin>>m>>n>>s;
	for(int i=1;i<=m;i++)cin>>c>>a[i]>>d[i];
	if(n==1)cout<<0;
	if(n==2)
	{
		if(s[0]==s[1])cout<<0;
		else cout<<min(min(d[1],d[2]),min(a[1],a[2]));
	}
    if(n==3)
	{
        if(s[0]==s[2])cout<<0;
		else if(s[0]!=s[1]&&s[1]!=s[2]&&s[1]!=s[3])cout<<min(d[1]+d[2],min(d[1]+a[2],d[2]+a[1]));
		else if(s[0]==s[1])cout<<min(d[2],d[1]+a[2]);
		else if(s[1]==s[2])cout<<min(d[1],d[2]+a[1]);
    }
	return 0;
}
